package org.melord.cap;

import java.io.File;
import java.io.IOException;

public class Cfg {
	private static Cfg instance = null;

	private String deviceName = "";
	private String deviceCapFilter = "tcp and ip";

	public String getDeviceCapFilter() {
		return deviceCapFilter;
	}

	public void setDeviceCapFilter(String deviceCapFilter) {
		this.deviceCapFilter = deviceCapFilter;
	}

	public String getDeviceName() {
		return deviceName;
	}

	public void setDeviceName(String deviceName) {
		this.deviceName = deviceName;
	}

	public static Cfg getInstance() {
		if (instance == null) {
			instance = new Cfg();
		}
		return instance;
	}

	private Cfg() {
		File file = new File(Constants.CFG_FILE);
		if (file.exists()) {
			IniEditor ini = new IniEditor();
			try {
				ini.load(file);
				setDeviceName(ini.get("device", "name"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void save() {
		File file = new File(Constants.CFG_FILE);
		if (file.exists()) {
			IniEditor ini = new IniEditor();

			try {
				ini.load(file);
				ini.set("device", "name", getDeviceName());
				ini.save(file);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public static final int TIME_UNIT_SEC = 0;
	public static final int TIME_UNIT_MIN = 1;
	public int timeUnit = TIME_UNIT_SEC;

	public int getTimeUnit() {
		return timeUnit;
	}

	public static void main(String[] args) {
		System.out.println(Cfg.getInstance().getDeviceName());
		Cfg.getInstance().setDeviceName("hello");
		Cfg.getInstance().save();
		System.out.println(Cfg.getInstance().getDeviceName());
	}
}
